package com.ncu.sports.controller;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.ncu.sports.pojo.Msg;
import com.ncu.sports.pojo.User;
import com.ncu.sports.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {


    @Autowired
    private UserService userService;

    @RequestMapping("/test")
    public String login1(){
        return "aaaa";
    }


    @RequestMapping("/login")
    public String loginForm(@RequestBody User user){ ;
//        System.out.println(user);
        int count = userService.getUserByUsernameAndPassword(user);
        Msg msg=new Msg();
        HashMap<Object,Object> map=new HashMap<>();
        if(count==1){
            msg.setFlag(1);
            msg.setMsg("操作成功");
            map.put("user",user);
            msg.setData(map);
        }else {
           msg.setMsg("操作失败");
           msg.setFlag(0);
           map.put("user",null);
           msg.setData(map);
        }
        String data = JSON.toJSONString(msg);
        return data;
    }

    @RequestMapping("/vagueSelectUser")
    public String vagueSelectUser(@RequestBody HashMap<Object,Object> maps){
        PageInfo<User> list = userService.getUserByUsername((String) maps.get("useInput"), (int)maps.get("currentPage"),(int)maps.get("pageSize"));
        Msg msg=new Msg();
        HashMap<Object,Object> map=new HashMap<>();
        if(list.getSize()==0){
            msg.setMsg("不存在该用户");
            msg.setFlag(0);
            map.put("user",null);
            msg.setData(map);

        }else {
            msg.setFlag(1);
            msg.setMsg("操作成功");
            map.put("user",list);
            msg.setData(map);
        }
        String data = JSON.toJSONString(msg);
        return data;

    }

    /*@RequestMapping("/AllUser")
    public String getAllUser(){
        PageInfo<User> list = userService.getUser(1, 3);
        Msg msg=new Msg();
        HashMap<Object,Object> map=new HashMap<>();
        if(list.getSize()==0){
            msg.setMsg("目前还没有用户!");
            msg.setFlag(0);
            map.put("user",null);
            msg.setData(map);

        }else {
            msg.setFlag(1);
            msg.setMsg("操作成功");
            map.put("user",list);
            msg.setData(map);
        }
        String data = JSON.toJSONString(msg);
        return data;
    }*/
    @RequestMapping("/updateState")
    public String updateState(@RequestBody User user){
        int count = userService.updateStateById(user);
        Msg msg=new Msg();
        if(count<=0){
            msg.setMsg("状态修改失败");
            msg.setFlag(0);


        }else {
            msg.setFlag(1);
            msg.setMsg("状态修改成功");
        }
        String data = JSON.toJSONString(msg);
        return data;
    }

    @RequestMapping("/insertUser")
    public String insertUser(@RequestBody User user){
        int count = userService.insertUser(user);
        Msg msg=new Msg();
        if(count<=0){
            msg.setMsg("用户添加失败");
            msg.setFlag(0);
        }else {
            msg.setFlag(1);
            msg.setMsg("用户添加成功");
        }
        String data = JSON.toJSONString(msg);
        return data;
    }

    @RequestMapping(value = "/deleteUser/{id}",method = RequestMethod.DELETE)
    public String deleteUser(@PathVariable("id") int id){
        int count = userService.deleteUser(id);
        Msg msg=new Msg();
        if(count<=0){
            msg.setMsg("用户删除失败");
            msg.setFlag(0);
        }else {
            msg.setFlag(1);
            msg.setMsg("用户删除成功");
        }
        String data = JSON.toJSONString(msg);
        return data;
    }

    @RequestMapping(value = "/getUpdateUser/{id}",method = RequestMethod.GET)
    public String getUpdateUser(@PathVariable("id") int id){
        User user = userService.getUpdateUser(id);
        Msg msg=new Msg();
        HashMap<Object,Object> map=new HashMap<>();
        if(user==null){
            msg.setMsg("用户查询失败");
            msg.setFlag(0);
            map.put("user",null);
            msg.setData(map);
        }else {
            msg.setFlag(1);
            msg.setMsg("用户查询成功");
            map.put("user",user);
            msg.setData(map);
        }
        String data = JSON.toJSONString(msg);
        return data;
    }


    @RequestMapping(value = "/updateUser", method = RequestMethod.PUT)
    public String updateUser(@RequestBody User user){
        int count = userService.updateUser(user);
        Msg msg=new Msg();
        if(count<=0){
            msg.setMsg("用户修改失败");
            msg.setFlag(0);
        }else {
            msg.setFlag(1);
            msg.setMsg("用户修改成功");
        }
        String data = JSON.toJSONString(msg);
        return data;
    }

    @RequestMapping(value = "/getUpdateRole/{id}",method = RequestMethod.GET)
    public String getUpdateRole(@PathVariable("id") int id){
        User role = userService.getUpdateRole(id);
        Msg msg=new Msg();
        HashMap<Object,Object> map=new HashMap<>();
        if(role==null){
            msg.setMsg("角色查询失败");
            msg.setFlag(0);
            map.put("role",null);
            msg.setData(map);
        }else {
            msg.setFlag(1);
            msg.setMsg("角色查询成功");
            map.put("user",role);
            msg.setData(map);
        }
        String data = JSON.toJSONString(msg);
        return data;
    }

    @RequestMapping(value = "/updateRole", method = RequestMethod.PUT)
    public String updateRole(@RequestBody User user){
        int count = userService.updateRole(user);
        Msg msg=new Msg();
        if(count<=0){
            msg.setMsg("用户修改失败");
            msg.setFlag(0);
        }else {
            msg.setFlag(1);
            msg.setMsg("用户修改成功");
        }
        String data = JSON.toJSONString(msg);
        return data;
    }
}
